package com.chunxiadong.service.impl;

import com.chunxiadong.entity.Blog;
import com.chunxiadong.entity.RecommendBlog;
import com.chunxiadong.mapper.BlogMapper;
import com.chunxiadong.entity.BlogBySearch;
import com.chunxiadong.mapper.TypeMapper;
import com.chunxiadong.service.BlogService;
import com.chunxiadong.service.TypeService;
import com.chunxiadong.utils.MarkdownUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @auther QiuShangcheng
 * @create 2021/6/7
 */
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    BlogMapper blogMapper;

    @Autowired
    TypeMapper typeMapper;

    @Override
    public List<RecommendBlog> getRecommendBlog(RecommendBlog blog) {

        return blogMapper.getRecommendBlog(blog);
    }

    @Transactional
    @Override
    public int addBlog(Blog blog) {
        blog.setCreateTime(new Date());
        return blogMapper.addBlog(blog);
    }

    @Transactional
    @Override
    public List<Blog> getAllBlog() {
        List<Blog> allBlog = blogMapper.getAllBlog();
        for (Blog blog : allBlog
        ) {
            blog.setContent(MarkdownUtils.markdownToHtmlExtensions(blog.getContent()));
            blog.setType(typeMapper.getType(blog.getTypeId()));
        }
        return allBlog;
    }

    @Transactional
    @Override
    public Blog getBlog(Integer id) {
        Blog blog = blogMapper.getBlog(id);
        blog.setContent(MarkdownUtils.markdownToHtmlExtensions(blog.getContent()));
        blog.setType(typeMapper.getType(blog.getTypeId()));
        return blog;
    }

    @Override
    public Blog getBlogToUpdate(Integer id) {
        Blog blog = blogMapper.getBlog(id);
        blog.setType(typeMapper.getType(blog.getTypeId()));
        return blog;
    }

    @Transactional
    @Override
    public int updateBlog(Blog blog) {
        return blogMapper.updateBlog(blog);
    }

    @Transactional
    @Override
    public int deleteBlogById(Integer id) {
        return blogMapper.deleteBlogById(id);
    }

    @Transactional
    @Override
    public List<Blog> getBlogByTypeAndTitle(BlogBySearch blog) {
        List<Blog> blogByTypeAndTitle = blogMapper.getBlogByTypeAndTitle(blog);
        for (Blog b : blogByTypeAndTitle
        ) {
            b.setContent(MarkdownUtils.markdownToHtmlExtensions(b.getContent()));
            b.setType(typeMapper.getType(b.getTypeId()));
        }
        return blogByTypeAndTitle;
    }
    
    @Transactional
    @Override
    public int IncreaseViews(Integer id) {
        return blogMapper.IncreaseViews(id);
    }

    @Override
    public List<Blog> getAllBlogToArchive() {
        return blogMapper.getAllBlogToArchive();
    }

    @Transactional
    @Override
    public PageInfo<Blog> getAllBlogByPage(Integer page, Integer rows) {
        //按照排序字段 倒序 排序
        String orderBy = "updateTime desc";
        PageHelper.startPage(page, rows, orderBy);
        List<Blog> allBlog = this.getAllBlog();
        PageInfo<Blog> pageInfo = new PageInfo<>(allBlog);
        return pageInfo;
    }

    @Transactional
    @Override
    public PageInfo<Blog> getAllBlogToAdminByPage(Integer page, Integer rows) {
        //按照排序字段 倒序 排序
        String orderBy = "updateTime desc";
        PageHelper.startPage(page, rows, orderBy);
        List<Blog> allBlog = this.getAllBlogToAdmin();
        PageInfo<Blog> pageInfo = new PageInfo<>(allBlog);
        return pageInfo;
    }

    @Transactional
    @Override
    public List<Blog> getAllBlogToAdmin() {
        List<Blog> allBlog = blogMapper.getAllBlogToAdmin();
        for (Blog blog : allBlog
        ) {
            blog.setType(typeMapper.getType(blog.getTypeId()));
        }
        return allBlog;
    }

    @Transactional
    @Override
    public PageInfo<Blog> getSearchBlogByPage(Integer page, Integer rows, BlogBySearch blog) {
        //按照排序字段 倒序 排序
        String orderBy = "updateTime desc";
        PageHelper.startPage(page, rows, orderBy);
        List<Blog> blogList= this.getBlogByTypeAndTitle(blog);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogList);
        return pageInfo;
    }
}
